package com.lhp.HttpDemo;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class HttpClient {

    private static Auth auth;

    public void scorePost() {
        // 创建默认的 httpClient 实例
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建 httpPost
        HttpPost httpPost = new HttpPost("http://124.232.163.147:8081/ScoringAPI/scoring/get_score");
        // 参数
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        tokenPost();
        // System.out.println(auth.getToken());
        params.add(new BasicNameValuePair("token", auth.getToken()));
        params.add(new BasicNameValuePair("assignment_title", "followU"));
        params.add(new BasicNameValuePair("assignment_content",
                "you raise me up.you are a cat.you.i am a cat.i am a cat.i am a cat.i am a cat.i am a cat.i am a cat.i am a cat .i am a cat ."));
        params.add(new BasicNameValuePair("user_grade", "4"));
        params.add(new BasicNameValuePair("province", "")); // 可不写
        UrlEncodedFormEntity uefEntity;
        try {
            uefEntity = new UrlEncodedFormEntity(params, "UTF-8");
            httpPost.setEntity(uefEntity);
            System.out.println("executing request : " + httpPost.getURI());
            CloseableHttpResponse response = httpClient.execute(httpPost);
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    System.out.println("--------------------------------------");
                    System.out.println("Response content : " + EntityUtils.toString(entity, "UTF-8"));
                    System.out.println("--------------------------------------");
                }
            } finally {
                response.close();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接，释放资源
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 发送 post请求访问本地应用并根据传递参数不同返回不同结果
     */
    public void tokenPost() {
        // 创建默认的 httpClient 实例
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建 httpPost
        HttpPost httpPost = new HttpPost("http://124.232.163.147:8081/ScoringAPI/auth");
        // 创建参数队列
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("appid", "tQSu9NCnTUSXoRWf"));
        formparams.add(new BasicNameValuePair("appkey", "E91EB7C89DAB923D802A56F8E08D00FC"));
        UrlEncodedFormEntity uefEntity;
        try {
            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httpPost.setEntity(uefEntity);
            System.out.println("executing request : " + httpPost.getURI());
            CloseableHttpResponse response = httpClient.execute(httpPost);
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    String message = EntityUtils.toString(entity, "UTF-8");
                    JSONObject jsonObject = JSON.parseObject(message);
                    auth = new Auth();
                    auth.setToken((String) jsonObject.get("token"));

                    System.out.println("--------------------------------------");
                    System.out.println("Response content : " + JSON.parseObject(message));
                    System.out.println("--------------------------------------");

                }
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接，释放资源
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
